package com.the_ring.chapter07;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import com.the_ring.chapter07.adapter.BillPagerAdapter;
import com.the_ring.chapter07.database.DatabaseHelper;

import java.util.Calendar;

public class BillPagerActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {

    private ImageView backIv;
    private TextView titleTv;
    private TextView actionTv;
    private TextView dateTv;
    private ViewPager billPager;
    private BillPagerAdapter billPagerAdapter;

    private Calendar calendar;

    private DatabaseHelper helper;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill_pager);

        backIv = findViewById(R.id.iv_back);
        titleTv = findViewById(R.id.tv_title);
        actionTv = findViewById(R.id.tv_action);
        dateTv = findViewById(R.id.tv_date);
        billPager = findViewById(R.id.vp_bill_pager);

        // 初始化数据
        titleTv.setText("帐单列表");
        actionTv.setText("添加账单");

        calendar = Calendar.getInstance();
        dateTv.setText(String.format("%s-%s", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));

        billPagerAdapter = new BillPagerAdapter(getSupportFragmentManager(), calendar.get(Calendar.YEAR));
        billPager.setAdapter(billPagerAdapter);
        billPager.setCurrentItem(calendar.get(Calendar.MONTH));
        billPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                calendar.set(Calendar.MONTH, position);
                dateTv.setText(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1));
            }
        });

        // 点击事件
        backIv.setOnClickListener(this);
        actionTv.setOnClickListener(this);
        dateTv.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        helper = DatabaseHelper.getInstance(this);
        helper.openWriteLink();
    }

    @Override
    protected void onPause() {
        super.onPause();
        helper.closeLink();
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        // 年份发生变化，更新适配器
        if (year != calendar.get(Calendar.YEAR)) {
            billPagerAdapter.setYear(year);
            billPagerAdapter.notifyDataSetChanged();
        }
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        dateTv.setText(year + "-" + (month + 1));

        billPager.setCurrentItem(month);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_date) {
            DatePickerDialog dialog = new DatePickerDialog(this, this,
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();
        } else if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_action) {
            Intent intent = new Intent(this, BillAddActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }
}